<?php

class Default_Model_ResultMapper
{
    protected $_dbTable;



    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Default_Model_DbTable_Result');
        }
        return $this->_dbTable;
    }

    public function save(Default_Model_Result $result)
    {
    	$auth = Zend_Auth::getInstance();
		$id = $auth->getIdentity()->spagooy_user_id;
    	$data = array(
//			'spagooy_result_date' => NOW(),
			'spagooy_result_user_id' => $id,
			'spagooy_result_ones' => $result->getOnes(),
			'spagooy_result_twos' => $result->getTwos(),
			'spagooy_result_threes' => $result->getThrees(),
			'spagooy_result_fours' => $result->getFours(),
			'spagooy_result_fives' => $result->getFives(),
			'spagooy_result_sixes' => $result->getSixes(),
			'spagooy_result_subsum' => $result->getSubsum(),
			'spagooy_result_bonus' => $result->getBonus(),
			'spagooy_result_one_pair' => $result->getPair(),
			'spagooy_result_two_pairs' => $result->getTwoPair(),
			'spagooy_result_three_of_a_kind' => $result->getThreeOfAKind(),
			'spagooy_result_four_of_a_kind' => $result->getFourOfAKind(),
			'spagooy_result_small_straight' => $result->getSmallStraight(),
			'spagooy_result_big_straight' => $result->getBigStraight(),
			'spagooy_result_full_house' => $result->getFullHouse(),
			'spagooy_result_chance' => $result->getChance(),
			'spagooy_result_yatzy' => $result->getYatzy(),
			'spagooy_result_total' => $result->getSum(),
        );

        if (null === ($id = $result->getId())) {
            unset($data['spagooy_result_id']);
            return $this->getDbTable()->insert($data);
		} else {
            $this->getDbTable()->update($data, array('spagooy_result_id = ?' => $id));
        }
    }
/*
    public function find($id, Default_Model_Guestbook $guestbook)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $guestbook->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created);
    }

    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Default_Model_Guestbook();
            $entry->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created)
                  ->setMapper($this);
            $entries[] = $entry;
        }
        return $entries;
    }
*/

}
